import Dialog from '@vant/weapp/dialog/dialog';
import {updateRepairStateById,updateRepairFinishTime} from "../../../../api/repair"
import {formatTime} from "../../../../utils/utils"

const app = getApp()
const {Toast} = app.globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {repairInfo} = options
    repairInfo = JSON.parse(repairInfo)
    let btnDisable = false
    if(repairInfo.state.indexOf("完成") != -1){
      btnDisable = true
    }
    this.setData({
      repairInfo,
      id:repairInfo._id,
      btnDisable
    })
  },

  // 确认订单是否完成
  toConfirm(){
    Dialog.confirm({
      title: '订单确认',
      message: '是否确认订单已完成？',
    }).then(async() => {
      let ret1 = await updateRepairStateById(this.data.id,"订单已完成")
      let ret2 = await updateRepairFinishTime(this.data.id,formatTime(Date.now()))
      if(ret1.code == 201 && ret2.code == 201){
        wx.navigateBack()
      }else{
        Toast.fail("确认失败")
      }
    }).catch(()=>{

    });
  },
  // 取消订单
  toCancel(){
    Dialog.confirm({
      title: '订单取消',
      message: '您的订单将被取消，是否继续？',
    }).then(async() => {
      let ret1 = await updateRepairStateById(this.data.id,"订单已取消")
      
      if(ret1.code == 201){
        wx.navigateBack()
      }else{
        Toast.fail("取消失败")
      }
    }).catch(()=>{

    });
  },
  // 打电话
  call(){
    console.log(this.data.repairInfo.receiverPhoneNumber);

    if(this.data.repairInfo.receiverPhoneNumber != "待定"){
      wx.makePhoneCall({
        phoneNumber: this.data.repairInfo.receiverPhoneNumber
      })
    }
  }
})